package com.ebt.m.proposal_v2.ui.function;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.ebt.m.proposal_v2.helper.WebViewCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * WebView管理栈，用于豁免险ExemptDao的保费计算模式管理
 *
 * @author archer.qi
 *         Created on 2017/7/17.
 */
public class WebViewStack {

    public static class WebViewObject {
        public int id;
        public String url;
        public WebView webview;
        public WebViewCallback callback;
    }

    private List<WebViewObject> mWebViewObjectStack;
    private int mAutoInCrementId; // 自增ID

    public List<WebViewObject> getWebViewObjectStack(){
        return mWebViewObjectStack;
    }

    public WebViewStack() {
        mWebViewObjectStack = new ArrayList<>();
    }

    public int size() {
        return mWebViewObjectStack == null ? 0 : mWebViewObjectStack.size();
    }

    public WebViewObject put(String url, WebView webView, WebViewCallback callback) {
        if (TextUtils.isEmpty(url) || webView == null) {
            return null;
        }
        mAutoInCrementId++;
        WebViewObject webViewObject = new WebViewObject();
        webViewObject.id = mAutoInCrementId;
        webViewObject.url = url;
        webViewObject.webview = webView;
        if (callback != null) {
            callback.setTag(url);
            callback.setId(mAutoInCrementId);
        }
        webViewObject.callback = callback;
        mWebViewObjectStack.add(webViewObject);

        return webViewObject;
    }

    public WebViewObject get(int id) {
        if (mWebViewObjectStack == null || mWebViewObjectStack.size() == 0 || id <= 0) {
            return null;
        }
        for (WebViewObject obj : mWebViewObjectStack) {
            if (obj != null && obj.id == id) {
                return obj;
            }
        }
        return null;
    }

    public WebViewObject get(String url) {
        if (mWebViewObjectStack == null || mWebViewObjectStack.size() == 0 || TextUtils.isEmpty(url)) {
            return null;
        }
        for (WebViewObject obj : mWebViewObjectStack) {
            if (obj != null && obj.url.equals(url)) {
                return obj;
            }
        }
        return null;
    }

    public WebViewObject get(int id, String url) {
        if (mWebViewObjectStack == null || mWebViewObjectStack.size() == 0) {
            return null;
        }
        if (id < 0 && TextUtils.isEmpty(url)) {
            return null;
        }
        if (id > 0 && TextUtils.isEmpty(url)) {
            return get(id);
        }
        if (id <= 0 && !TextUtils.isEmpty(url)) {
            return get(url);
        }
        for (WebViewObject obj : mWebViewObjectStack) {
            if (obj != null && obj.url.equals(url) && obj.id == id) {
                return obj;
            }
        }
        return null;
    }

    public synchronized void remove(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        removeWebViewObject(get(url));
    }

    public void remove(int id) {
        if (id <= 0) {
            return;
        }
        removeWebViewObject(get(id));
    }

    public void remove(int id, String url) {
        if (id <= 0 && TextUtils.isEmpty(url)) {
            return;
        }
        removeWebViewObject(get(id, url));
    }

    public void remove(WebViewObject obj) {
        if (obj == null) {
            return;
        }
        removeWebViewObject(obj);
    }

    public void clearAll() {
        if (mWebViewObjectStack == null) {
            return;
        }
        if (mWebViewObjectStack.size() == 0) {
            mWebViewObjectStack = null;
            return;
        }
        for (WebViewObject obj : mWebViewObjectStack) {
            if (obj != null) {
                remove(obj);
            }
        }
        mWebViewObjectStack = null;
    }

    private void removeWebViewObject(WebViewObject obj) {
        if (obj != null) {
            mWebViewObjectStack.remove(obj);
            if (obj.callback != null) {
                obj.callback.without();
            }
            if (obj.webview != null) {
                destroyWebView(obj.webview);
            }
        }
    }

    private void destroyWebView(WebView webView) {
        if (webView == null) {
            return;
        }
        ViewGroup parent = (ViewGroup) webView.getParent();
        if (parent != null) {
            parent.removeView(webView);
        }
        webView.loadUrl("about:blank");
        webView.clearCache(true);
        webView.clearHistory();
        webView.clearMatches();
        webView.clearFormData();
        webView.destroy();
        webView = null;
    }
}
